package org.zjt.thread;

import java.util.function.Function;
import java.util.function.Supplier;

/**
 * Description:
 *
 * @author juntao.zhang
 * Date: 2018-05-31 下午3:37
 * @see
 */
public class Try {

    public static <T> Supplier<T> of(WrapperSupplier<T>  wrapperSupplier){
        return () ->{
            try {
                return wrapperSupplier.get();
            } catch (Throwable throwable) {
                throw new RuntimeException(throwable);
            }
        };
    }

    public static  Runnable of(WrapperRunnable wrapperRunnable){
        return () ->{
            try {
                wrapperRunnable.run();
            } catch (Throwable throwable) {
                throw new RuntimeException(throwable);
            }
        };
    }


    public static <T, R> Function<T, R> of(WrapperFunction<T, R>   wrapperFunction){
        return a ->{
            try {
                return wrapperFunction.apply(a);
            } catch (Throwable throwable) {
                throw new RuntimeException(throwable);
            }
        };
    }

     @FunctionalInterface
    public interface WrapperSupplier<T> {

        /**
         * Gets a result.
         *
         * @return a result
         */
        T get() throws Throwable;
    }

    @FunctionalInterface
    public interface WrapperRunnable{

        /**
         * Gets a result.
         *
         * @return a result
         */
        void run() throws Throwable;
    }



    @FunctionalInterface
    public interface WrapperFunction<T, R> {

        R apply(T t) throws Throwable;

    }
}





